"""
语义一致性流式处理示例 - 展示基于buffer_size的智能缓存控制
重构版本：基于Linus设计哲学，从754行简化到50行
"代码行为要与承诺一致，不要让用户猜测"
"""

from gua import GuaApp
from gua.streaming_utils import create_buffer_demo_stream
from gua.template_utils import respond_template

app = GuaApp()


@app.route("/")
async def index():
    """语义一致性流式处理演示首页 - Linus式重构：使用模板"""
    return respond_template("semantic_streaming.html")


@app.route("/realtime-stream")
async def realtime_stream():
    """实时流演示 - Linus式重构：使用通用缓冲演示"""
    return create_buffer_demo_stream(0, "⚡ 实时流演示")


@app.route("/small-buffer-stream")
async def small_buffer_stream():
    """小缓冲流演示 - Linus式重构：使用通用缓冲演示"""
    return create_buffer_demo_stream(4096, "📦 小缓冲流演示")


@app.route("/medium-buffer-stream")
async def medium_buffer_stream():
    """中等缓冲流演示 - Linus式重构：使用通用缓冲演示"""
    return create_buffer_demo_stream(16384, "📦 中等缓冲流演示")


@app.route("/large-buffer-stream")
async def large_buffer_stream():
    """大缓冲流演示 - Linus式重构：使用通用缓冲演示"""
    return create_buffer_demo_stream(65536, "📦 大缓冲流演示")


@app.route("/buffer-demo/{buffer_size}")
async def buffer_demo_with_size(buffer_size: str):
    """带参数的缓冲效果演示 - Linus式简化实现"""
    try:
        buffer_size_int = int(buffer_size)
    except ValueError:
        buffer_size_int = 16384  # 默认平衡模式
    
    return create_buffer_demo_stream(buffer_size_int, f"缓冲演示 (buffer_size={buffer_size_int})")


if __name__ == "__main__":
    import subprocess
    import sys
    
    # 使用gua命令显示示例启动信息
    result = subprocess.run([sys.executable, "-m", "gua.cli", "examples.semantic_streaming:app", "--example-info"],
                          capture_output=True, text=True)
    if result.returncode == 0:
        print(result.stdout)
    else:
        print("错误: 无法显示启动信息")
        print("请手动运行: gua examples.semantic_streaming:app --example-info")